﻿/**
 * autor Zero
 * version 1.4
 * Fijarse el tema este del tiempo de espera que deja a Java cambiar la base de datos.
 * Quizá sería mejor de otra manera hacerlo (la extensión debería refrescarse sola cuando le llegan nuevos mensajes de texto)
 * Ya manda mensajes de texto!
 * Ahora chequea si el type corresponde a alguno de los types posibles
 * Esta clase está como loca. Ahora switchea el selected() y dispara cosas por todos los wines  */

import com.dinmax.flash.central.bridge.*
import com.dinmax.flash.draw.cabeza.*
import com.dinmax.flash.central.extension.message.*
import com.dinmax.flash.central.extension.user.Contact

class com.dinmax.flash.central.bridge.DATABridge
extends ControlCabeza
implements SelectedListener {
	
	private static var TIME_WAIT:Number = 600; //en milisegundos
	
	//framenames
	public static var USER:String = "user";
	public static var USER_STOP:String = "userStop";
	public static var CONTACT_ABM:String = "contactABM";
	public static var LOG:String = "log";
	public static var MESSAGES:String = "messages";
	public static var MESSAGES_STOP:String = "messagesStop";
	public static var MESSAGE_DETAIL:String = "messageDetail";
	public static var MESSAGE_SENDER:String = "messageSender";
	public static var MESSAGE_UNAUTORIZED:String = "messageUnautorized";
	public static var VOICEMAILS:String = "voicemails";
	public static var VOICEMAILS_STOP:String = "voicemailsStop";
	public static var VOICEMAIL_DETAIL:String = "voicemailDetail";
	public static var VOICEMAIL_UNAUTORIZED:String = "voicemailUnautorized";
	public static var PREF:String = "preferences";
		
	private var parent:MovieClip;
	private var mcTransparency:MovieClip;
	private var mcBtnData:MovieClip;
	//ESTA ES LA MAS IMPORTANTE, porque define que tipo de pantalla se abrió
	private var type:String;
	private var value:Object;

	//user
	private var mcPersonalData:USERDataBridge;
	private var mcAddContact:ButtonCabeza;
	private var mcWindowContact:WINDOWContactBridge;
	private var mcContactABM:ContactABMBridge;
	
	//message
	private var mcMessageDetail:MESSAGEDetailBridge;
	private var mcMessageSender:MESSAGESenderBridge;
	private var mcWindowMessage:WINDOWMessageBridge;
	private var mcSend:MovieClip;
	private var txt_title:TextField;
	
	//voicemail
	private var mcWindowVoiceMessage:WINDOWVoiceMessageBridge;
	private var mcVoicemailDetail:VOICEMAILDetailBridge;
		
	//log
	private var windowLog:WINDOWLogBridge;
	
	//messages
	//private var mcMessages:com.dinmax.flash.central.bridge.MESSAGEBridge;
	
	function DATABridge(){
		parent = this._parent;
	};
	
	public function getParent():MovieClip {
		return this.parent;
	};
	
	function onLoad():Void{
		//trace("DATABridge.as | onLoad()");
		var myDropFilter = new flash.filters.DropShadowFilter();
		myDropFilter.alpha = .5;
		
		var aFilters:Array = this.filters;
		aFilters.push(myDropFilter);
		this.filters = aFilters;
		this.gotoAndPlay(type);
	};
	
	public function initChild(child:MovieClip):Void {
		switch (child){
			case this.mcAddContact:
			this.mcAddContact.addListener(this);
			break;
			
			case this.mcWindowMessage:
			//trace("mcWindowMessage.as | initChild()");
			break;
		};
	};
				
	public function endInterpolation():Void {
		//trace("DATABridge.as | endInterpolation() type= "+type);
		var root:DATABridge = this;
		this.enabled = true;
		mcTransparency.onRollOver=function():Void {
			this.useHandCursor=false;
		};
				
		mcBtnData.onPress=function(){
			root.getParent().clearData(root);
			root.getParent().dataLoaded();
		};
		switch(type){
			case DATABridge.USER:
			//trace("DATABridge.as | typeUser ");
			parent.getUserData();
			this.mcWindowContact.deleteItems();
			parent.getUserContacts();
			break;
			
			case DATABridge.LOG:
			//trace("DATABridge.as | typeLog ");
			parent.getLog();
			break;
			
			case DATABridge.MESSAGES:
			//trace("DATABridge.as | typeMessages ");
			root.refreshMessages();
			
			mcSend.onPress=function():Void {
				CENTRALBridge.getActive().writeMessage();
				return;
			};
			break;
			
			case DATABridge.MESSAGE_SENDER:
			//trace("DATABridge.as | type MESSAGE_SENDER me mandaron para acá");
			if(this.getValue() != undefined){
				this.mcMessageSender.setTo(Number(this.getValue()));
				this.mcMessageSender.setListener(this);
			};
			break;
			
			case DATABridge.VOICEMAILS:
			//trace("DATABridge.as | type voicemails");
			txt_title.text = "Mensajes de voz ("+ parent.getNewVoiceMessages() +" nuevos)";
			this.mcWindowMessage.deleteItems();
			parent.getVoiceMessages();
			break;
			
			case DATABridge.PREF:
			//trace("DATABridge.as | type preferences");
			break;
						
			default:
			throw new Error("DATABridge.as | endInterpolation() | ERROR= "+type);
			break;
		}
		
	};
	public function refreshData():Void {
		this.endInterpolation();
		return;
	};
	public function refreshMessages():Void {
			txt_title.text = "Mensajes de texto ("+ parent.getNewTextMessages() +" nuevos)";
			this.mcWindowMessage.deleteItems();
			parent.getTextMessages();
		return;
	};
	
	public function setUserData(nick:String, name:String, lastName:String, mail:String):Void {
		this.mcPersonalData.setNick(nick);
		this.mcPersonalData.setName(name + " " + lastName);
		this.mcPersonalData.setMail(mail);
		return;
	};
	
	public function addContact(cnt:Contact):Void{
		this.mcWindowContact.setLayout(new WindowCabezaLayout(mcWindowContact));
		var clip:MovieClip = this.mcWindowContact.drawItem(cnt.getPhone(), cnt.getNick(), cnt.getEmail(), cnt.getVisibility());
		clip.setValue(cnt);
		clip.addListener(this);
		return;
	};
	
	public function addLog(time:Date, type:String, description:String):Void {
		this.windowLog.setLayout(new WindowCabezaLayout(windowLog));
		this.windowLog.drawItem(time, type, description);
		return;
	};
	
	public function resetLog():Void {
		this.windowLog.deleteItems();
	};
		
	public function addVoiceMessage(msg:VoiceMessage):Void{
		//trace("DATABridge.as addVoiceMessage()");
		this.mcWindowVoiceMessage.setLayout(new WindowCabezaLayout(this.mcWindowVoiceMessage));
		var clip:MovieClip = this.mcWindowVoiceMessage.drawItem(msg.getTime(), msg.getCallerid(), msg.getMessageURL(), msg.getState());
		clip.setValue(msg);
		clip.setListener(this);
		return;
	};
	public function addTextMessage(msg:TextMessage):Void{
		this.mcWindowMessage.setLayout(new WindowCabezaLayout(this.mcWindowMessage));
		var clip:MovieClip = this.mcWindowMessage.drawItem(msg.getTime(), String(msg.getFrom()), msg.getSubject(), msg.getState());
		clip.setValue(msg);
		clip.setListener(this);
		
		return;
	};
	
	public function displayMessageSender():Void {
		this.gotoAndStop(DATABridge.MESSAGE_SENDER);
		var root:DATABridge = this;
		mcMessageSender.onLoad=function():Void {
			this.setListener(root);
			return;
		};
		return;
	};
	
	public function sendMessage(to:Number, subject:String, body:String):Void {
		parent.sendMessage(to, subject, body);
		return;
	};
	
	public function displayContactABM(cnt:Contact):Void {
		this.gotoAndStop(DATABridge.CONTACT_ABM);
		var root:DATABridge = this;
		mcContactABM.onLoad = function():Void {
			this.setValue(cnt);
			this.addListener(root);
			this.display();
			return;
		};	
	};
	
	public function displayTextMessage(msg:TextMessage):Void {
		if(parent.isExtensionAutorized()){
			this.gotoAndStop(DATABridge.MESSAGE_DETAIL);
			var root:DATABridge = this;
			mcMessageDetail.onLoad=function():Void{ 
				this.display(msg.getTime(), String(msg.getFrom()), msg.getSubject(), msg.getBody());
				this.setListener(root);
				this.setValue(msg);
				return;
			};
			if(msg.getState() == Message.NEW) { parent.readMessage(msg)};
		} else {
			this.displayUnautorized(DATABridge.MESSAGE_UNAUTORIZED, DATABridge.MESSAGES_STOP);
		};
		return;
	};
	
	public function displayVoiceMail(msg:VoiceMessage):Void {
		if(parent.isExtensionAutorized()){
			this.gotoAndStop(DATABridge.VOICEMAIL_DETAIL);
			var root:DATABridge = this;
			mcVoicemailDetail.onLoad=function():Void{ 
				this.display(msg.getTime(), msg.getCallerid());
				this.setListener(root);
				this.setValue(msg);
				return;
			};
			//if(msg.getState() == Message.NEW) { parent.refreshVoiceMails(msg)}; // esto habría que verlo
		} else {
			this.displayUnautorized(DATABridge.MESSAGE_UNAUTORIZED, DATABridge.MESSAGES_STOP);
		};
		return;
	};
	
	public function refreshVoiceMessages():Void {
		parent.refreshVoiceMessages();	
	};
	
	public function displayUnautorized(type:String, type2:String):Void {
		this.gotoAndStop(type);
		this.waitAndDisplay(type2);
		return;
	};
	
	public function closeDisplay(msg:Message):Void {
		
		if(msg != undefined) parent.deleteMessage(msg);
				
		this.enabled = false;
		
		this.waitAndDisplay(this.getType());
		return;
	};
		
	public function waitAndDisplay(type:String):Void {
		var nTimer:Number = getTimer();
		var nTimeOut:Number = DATABridge.TIME_WAIT;
		this.onEnterFrame=function():Void{
			if(getTimer() - nTimer > nTimeOut) {
				delete this.onEnterFrame;
				this.gotoAndPlay(this.getType());
			}
			return;
		};
		return;
	};
	
	public function closeDataBridge():Void {
		this.gotoAndStop(1);
		return;
	};
	
	public function selected(control:ControlCabeza):Void {
		switch(control){
			case this.mcAddContact:
			this.displayContactABM();
			break;
			
			case this.mcContactABM:
			//trace("DATABridge.as mcContactABM selected()");
			//trace("DATABridge.as "+control.getValue());
			if(Contact(control.getValue())){
				CENTRALBridge.getActive().saveContact(parent, Contact(control.getValue()));
			} else if (Number(control.getValue())) {
				CENTRALBridge.getActive().deleteContact(Number(control.getValue()));
			} 
			this.waitAndDisplay(DATABridge.USER_STOP);
			break;	
							
		};
		if(control instanceof CONTACTItemBridge){
			//trace("DATABridge.as CONTACTItemBridge selected()");
			this.displayContactABM(Contact(control.getValue()));
		}
		return;
	};
	
	public function setType(type:String):Void {
		var check:Boolean = type == DATABridge.USER || type == DATABridge.LOG || type == DATABridge.MESSAGES || type == DATABridge.MESSAGE_SENDER || type == DATABridge.VOICEMAILS || type == DATABridge.PREF; 
		//trace("DATABridge.as setType() type= "+type);
		if(check) {
			this.type = type;
		} else {
			throw new Error("ERROR= DATABridge.as setType() TYPE INCORRECTO");
		};
		return;
	};
	
	public function getType():String {
		return this.type;
	};
	
	public function getValue():Object {
		return value;
	}

	public function setValue(value:Object):Void {
		this.value = value;
		return;
	}

}